package org.busalerter.ui;

import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.GlazedLists;
import ca.odell.glazedlists.swing.EventTableModel;
import org.busalerter.domain.BusStop;
import org.busalerter.domain.BusTime;
import org.busalerter.parser.DataScraper;
import org.busalerter.parser.DataScraperImpl;
import static org.busalerter.ui.SwingUtils.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * Created by IntelliJ IDEA.
 * <p/>
 * User: bethella
 * Date: 18/11/11
 * Time: 17:04
 */
public class MainFrame extends JFrame {

    private JButton refreshButton;
    private BusStopSelectorDialog busStopSelectorDialog;
    private JButton selectBusStopButton;

    private EventTableModel<BusTime> busTimesTableModel;
    private EventList<BusTime> busTimes;

    private DataScraper dataScraper;

    private BusStop currentBusStop;


    public MainFrame() {
       //Create and set up the window.
        this.setTitle("Bus Times");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        busStopSelectorDialog = new BusStopSelectorDialog(this);

        // set default bus-stop
        currentBusStop = busStopSelectorDialog.getCurrentBusStop();

        selectBusStopButton = new JButton(currentBusStop.getName());
        selectBusStopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                busStopSelectorDialog.setLocationRelativeTo(MainFrame.this);

                busStopSelectorDialog.open();

                // update mainpanel with current busstop info
                currentBusStop = busStopSelectorDialog.getCurrentBusStop();
                dataScraper.updateBusStop(currentBusStop);
                busTimes.clear();
                busTimes.addAll(currentBusStop.getBusTimes());

                // update button with current bus-stop name
                selectBusStopButton.setText(currentBusStop.getName());

            }
        });
        this.add(selectBusStopButton, BorderLayout.NORTH);





        dataScraper = new DataScraperImpl();


        dataScraper.updateBusStop(currentBusStop);

        // load contents into table
        busTimes = GlazedLists.eventList(currentBusStop.getBusTimes());
        busTimesTableModel = new EventTableModel<BusTime>(busTimes, new BusTimeTableFormat());
        JTable busTimesTable = new JTable(busTimesTableModel);
        busTimesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        JScrollPane busTimesTableScrollPane = new JScrollPane(busTimesTable);


         // add button to refresh bustimes
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        refreshButton = new JButton("Last Updated: " + dateFormat.format(System.currentTimeMillis()));
        refreshButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                dataScraper.updateBusStop(currentBusStop);
                busTimes.clear();
                busTimes.addAll(currentBusStop.getBusTimes());


                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

                // specify last updated
                refreshButton.setText("Last Updated: " + dateFormat.format(System.currentTimeMillis()));
            }
        });

        //this.add(busID, BorderLayout.NORTH);
        this.add(busTimesTableScrollPane);
        this.add(refreshButton, BorderLayout.SOUTH);



        int delay = 5000; //milliseconds
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                //...Perform a task...

                dataScraper.updateBusStop(currentBusStop);
                busTimes.clear();
                busTimes.addAll(currentBusStop.getBusTimes());

                // specify last updated
                DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
                refreshButton.setText("Last Updated: " + dateFormat.format(System.currentTimeMillis()));
            }
        };

        new Timer(delay, taskPerformer).start();

        //Display the window.
        packAndShowOnScreenCenter(this);

    }


}